<?php
namespace app\admin\controller;
use think\Db;
class Index extends Common 
{

    public function index()
    {
        $admin = db('admin')->field('loginnum')->where('adminid',session('adminid'))->find();
        //系统信息
        $version = Db::query('SELECT VERSION() AS ver');

        $config = [
            'url'             => $_SERVER['HTTP_HOST'],
            'document_root'   => $_SERVER['DOCUMENT_ROOT'],
            'server_os'       => PHP_OS,
            'server_port'     => $_SERVER['SERVER_PORT'],
            'server_ip'       => $_SERVER['SERVER_ADDR'],
            'server_soft'     => $_SERVER['SERVER_SOFTWARE'],
            'php_version'     => PHP_VERSION,
            'mysql_version'   => $version[0]['ver'],
            'logintime'       => date('Y-m-d H:i:s',session('logintime')),
            'loginnum'        => $admin['loginnum'],
        ];

        /**
         * 全部设备 -1全部1在线0离线
         */
        //$device['all'] = $this->device(-1);
        $device['all'] = 0;

        /**
         * 在线设备
         */
       // $device['online'] = $this->device(1);
        $device['online'] = 0;

        /**
         * 离线设备
         */
        //$device['offline'] = $this->device(0);
        $device['offline'] = 0;


        //账号管理
        $list = counts('trill_account','addtime');

        /**
         * 平台数据统计
         */
        $infos['usercount'] = db('user')->count();
        $infos['usertoday'] = db('user') ->whereTime('addtime', 'today')->count();
        $this->assign('infos',$infos);
        $this->assign('list',$list);
        $this->assign('device',$device);
        $this->assign('config',$config);
        return $this->fetch();
    }

    /**
     * 图表主页
     */
    public function chart()
    {
        //账号管理
        $list = counts('trill_account','addtime');
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 设备列表
     */
    public function device($type)
    {
        $config = configs('api');
        $url = $config['apiurl'].'/openapi/equipment/list';
        $array = [
            'appkey' => $config['apiappkey'],
            'limit' => 10,
            'page' => 1,
            'type' => $type,
        ];
        //$model = model('Common')->curl_post($url,$array);
        $model = model('Common')->curl_post($url,$array);
        $data = json_decode($model,true);
        if($data['code'] != 200){
            $data['count'] = 0;
        }else{
            $data['count'] = $data['count'];
        }
        return $data['count'];
    }

    public function indexs()
    {
    	$str = "/layuiadmin/public/index.php/admin/wechat/menu.html";
    	echo substr($str,strpos($str, '/admin/')+7);
    	
        return $this->fetch();
    }
    
    //清除缓存
    public function clear()
    {
        $dir = '../runtime';   
        deleteDir($dir);
        adminlog();
        return json(['code' => 1, 'msg' => '清除成功']);
        /*$this->success('清除成功','admin/index/index',0,1);*/
    }

    public function no()
    {
        return $this->fetch('public/no');
    }
}
